<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

</head>
<body>
<button>get</button>
<div style="width: 1000px;height: 50px;border: 1px red solid"></div>
<button>post</button>
<button>put</button>
<button>delete</button>
<script>
    axios.defaults.baseURL = 'http://127.0.0.1:3000'
    // axios.defaults.method = 'post'
    // axios.defaults.params = {id:2}
    const btns = document.querySelectorAll('button');

    // 使用axios.create创建的对象用法和axios类似,在请求不同的服务器的时候适用
    const a1 = axios.create({
        method: 'get',
        baseURL:'http://127.0.0.1:3000/'
    })
    const a2 = axios.create({
        method: 'get',
        baseURL:'http://127.0.0.1:3001/'
    })
    a1({
        url: 'posts'
    }).then(value => {
        console.log(value)
    })

    btns[0].onclick = function (){
        // axios({
        //     method: 'get',
        //     url: '/posts',
        // }).then(value => {
        //     console.log(value)
        //     document.querySelectorAll('div')[0].innerHTML = JSON.stringify(value.data)
        //
        // })
        axios.get('/posts',).then(value => {
            console.log(value)
            document.querySelectorAll('div')[0].innerHTML = JSON.stringify(value.data)

        })
    }
    btns[1].onclick = function (){
        axios({
            method: 'post',
            url: '/posts',
            data:{
                "title": "阿巴阿巴",
                "author": "阿巴",
            }
        }).then(value => {
            console.log(value)
        })
    }
    btns[2].onclick = function (){
        axios({
            method: 'put',
            url: 'posts/3',
            data: {
                "title": "乌拉乌拉",
                "author": "乌拉",
            }
        }).then(value => {
            console.log(value)
        })
    }
    btns[3].onclick = function (){
        axios({
            method: 'delete',
            url: 'posts/4',
        }).then(value => {
            console.log(value)
        })
    }
</script>
</body>
</html>